package cn.itsource.basic.service.impl;

import cn.itsource.basic.mapper.BaseMapper;
import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.service.IBaseService;
import cn.itsource.basic.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;
//                |__DepartmentMapper
//                     |__DepartmentMapper的代理实现类
//                |__EmployeeMapper
//                     |__EmployeeMapper的代理实现类
//    注意：BaseMapper必须要加泛型，否则底层注册这个接口，会有多个实现类，就会报异常(没有唯一bean的异常)

    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    @Override
    @Transactional
    public void batchRemove(List<T> ts) {
        baseMapper.batchRemove(ts);
    }

    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    @Override
    public PageBean<T> queryPage(BaseQuery query) {
        Long count = baseMapper.queryCount(query);
        //如果没有数据 ，可以省略一次查询
        if (count==null || count <=0){
            return  new PageBean<T>();
        }
        List<T> list = baseMapper.queryData(query);

        return new PageBean<T>(count,list);
    }


}
